const app = getApp()

Page({
  data: {
    lottery_list: [],
    curpage: 1,
    hasMore: true,
    page_total: 1
  },
  onLoad: function () {
    var that = this;
    that.getLottery();
  },

  /**
   * 获取抽奖
   */
  getLottery: function () {
    var _this = this;
    tt.showLoading({
      title: '加载中...',
      mask: true
    });

    var that = this;
    tt.request({
      url: app.globalData.url + 'index.php?act=toutiao_index&op=index',
      data: {
        curpage: _this.data.curpage
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        tt.hideLoading({});
        if (res.data.code == 200) {
          if (res.data.page_total >= _this.data.curpage) {
            var data = _this.data.lottery_list;
            var curpage = _this.data.curpage
            data = data.concat(res.data.datas.list);
            _this.setData({
              lottery_list: data,
              hasMore: res.data.hasmore,
              more: true
            })
          } else {
            _this.setData({
              hasMore: res.data.hasmore,
              more: false
            })
          }
          curpage++;
          _this.setData({
            curpage: curpage
          })
        }
      },
      fail(res) {
        console.log(`request调用失败`);
      }
    })

  },
   /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.hasMore) { //是否还有更多数据
      //获取数据
      this.getLottery();
    }else{
      wx.showToast({
        title:'暂无更多数据',
        icon:'none'
      });
    }
  },
})
